function AccountsFilter2(srcAccount, dstAccount) {
	this.srcAccount = srcAccount;
	this.dstAccount = dstAccount;
}

function TrFilter2() {

	this.applyImmidiately = true;
	this.showIncomes = true;
	this.showOutcomes = true;
	this.showTransfers = true;
	this.approvedOnly = false;
}

function TrFilterManager(updateCallback, filterDiv, applyImmidiatelyDiv, applyButtonDiv,
                            showIncomesDiv, showOutcomesDiv, showTransfersDiv, approvedOnlyDiv,
                            dateFromDiv, dateToDiv, accountSrcDiv, accountDstDiv,
                            accountsSwapBtnDiv, accountsAddBtnDiv, accountsDiv, categoryDiv,
                            categoryTreeDiv, categoriesDiv, tagsDiv
    ) {
    this.filterDiv = filterDiv;
    this.applyImmidiatelyDiv = applyImmidiatelyDiv;
    this.applyButtonDiv = applyButtonDiv;
    this.showIncomesDiv = showIncomesDiv;
    this.showOutcomesDiv = showOutcomesDiv;
    this.showTransfersDiv = showTransfersDiv;
    this.approvedOnlyDiv = approvedOnlyDiv;
    this.dateFromDiv = dateFromDiv;
    this.dateToDiv = dateToDiv;
    this.accountSrcDiv = accountSrcDiv;
	this.srcAccountCombo = null;
	this.accountsSwapBtnDiv = accountsSwapBtnDiv;
    this.accountDstDiv = accountDstDiv;
	this.dstAccountCombo = null;
	this.accountsAddBtnDiv = accountsAddBtnDiv;
	this.accountsDiv = accountsDiv;
	this.accountsFilter = new Array();
    this.categoryDiv = categoryDiv;
	this.categoryTreeDiv = categoryTreeDiv;
	this.categoryCombo = null;
	this.categoriesDiv = categoriesDiv;
    this.tagsDiv = tagsDiv;
	this.tagsCloud = null;

	this.value = new TrFilter();

    this.init = function(book) {
        var self = this;
        $(this.filterDiv).jqxExpander({width: '99.5%'});
        $(this.applyImmidiatelyDiv).jqxCheckBox({ checked: true});
        $(this.applyImmidiatelyDiv).on('change', function (event) {
            self.value.applyImmidiately = event.args.checked;
            if(self.value.applyImmidiately) {
                self.apply();
            }
        });

        $(this.applyButtonDiv).jqxButton();
        $(this.applyButtonDiv).on('click', function() {
            self.apply();
        });

        $(this.showIncomesDiv).jqxCheckBox({ checked: this.value.showIncomes});
        $(this.showOutcomesDiv).jqxCheckBox({ checked: this.value.showOutcomes});
        $(this.showTransfersDiv).jqxCheckBox({ checked: this.value.showTransfers});

        $(this.approvedOnlyDiv).jqxCheckBox({ checked: this.value.approvedOnly});

        $(this.dateFromDiv).jqxDateTimeInput({ formatString: 'dd.MM.yyyy', width: '100px' });
        $(this.dateToDiv).jqxDateTimeInput({ formatString: 'dd.MM.yyyy', width: '100px'});

	    this.srcAccountCombo = new AccountCombo(this.accountSrcDiv, book.accounts);
	    this.srcAccountCombo.render();
	    this.dstAccountCombo = new AccountCombo(this.accountDstDiv, book.accounts);
	    this.dstAccountCombo.render();
	    $(this.accountsSwapBtnDiv).jqxButton({width: 20});
	    $(this.accountsSwapBtnDiv).on('click', function () {
		    self.swapAccounts();
	    });
	    $(this.accountsAddBtnDiv).jqxButton({ width: 20 });
	    $(this.accountsAddBtnDiv).on('click', function () {
		    self.addAccountsFilter();
	    });

	    this.categoryCombo = new CategoryCombo(this.categoryDiv, this.categoryTreeDiv, book.categories, this);
	    this.categoryCombo.render();

	    this.tagsCloud = new TagsCloud(this.tagsDiv, book.tags);
	    this.tagsCloud.render();

        /*
         $("#trFilterDateRange").jqxRangeSelector({
         width: 500, height: 10, min: new Date(2014, 0, 1), max: new Date(2014, 11, 31),
         range: { from: new Date(2014, 3, 15), to: new Date(2014, 7, 29) },
         labelsOnTicks: false,
         majorTicksInterval: "month", minorTicksInterval: "day", labelsFormat: 'MMM', markersFormat: 'dd MMM yyyy',
         moveOnClick: true
         });*/
    };

	this.load = function() {

	};

	this.updateAccounts = function () {
		var self = this;
		var html = '<table>';
		var divId;
		var count = 0;
		var divIds = new Array();
		this.accountsFilter.forEach(function(filter) {
			divId = self.accountsDiv.slice(1) + '_' + (count++);
			divIds.push(divId);
			html += '<tr><td>' + self.formatAccount(filter.srcAccount) + '</td><td>&rarr;</td><td>' + self.formatAccount(filter.dstAccount) + '</td>' +
				'<td><div id="' + divId + '">x</div></td></tr>';
		});
		html += '</table>';
		$(this.accountsDiv).html(html);
		count = 0;
		divIds.forEach(function(divId) {
			$('#' + divId).jqxButton({ width: 10, height: 10 });
			$('#' + divId).on('click', function() {
				var parts = divId.split('_');
				var index = parseInt(parts[parts.length - 1]);
				self.accountsFilter.splice(index, 1);
				self.updateAccounts();
			});
			count++;
		});
	};

	this.formatAccount = function(account) {
		return account.id < 0? account.name: '<b>' + account.name + '</b>';
	}

	this.addAccountsFilter = function() {
		var srcAccount = this.srcAccountCombo.value;
		var dstAccount = this.dstAccountCombo.value;
		if(srcAccount.id !== dstAccount.id) {
			var exists = false;
			this.accountsFilter.forEach(function (filter) {
				if (filter.srcAccount.id == srcAccount.id && filter.dstAccount.id == dstAccount.id) {
					exists = true;
					return false;
				}
			});
			if (!exists) {
				this.accountsFilter.push(new AccountsFilter2(srcAccount, dstAccount));
			}
			this.updateAccounts();
		}
	};

	this.swapAccounts = function() {
		var srcAccount = this.srcAccountCombo.value;
		var dstAccount = this.dstAccountCombo.value;
		this.dstAccountCombo.select(srcAccount.id);
		this.srcAccountCombo.select(dstAccount.id);
	};

	this.refreshCategories = function(categoryCombo) {
		var self = this;
		var html = '';
		var br = ''
		categoryCombo.value.forEach(function(category) {
			html += br + category.fullName();
			br = '<br>';
		});
		$(this.categoriesDiv).html(html);
	};

    this.apply = function() {
        console.log('applying');
	    updateCallback();
    };

	this.getValue = function() {
		return null;
	};
}